/*
 * Copyright 2020-2025 the original author or authors.
 * You cannot use this file unless authorized by the author.
 */

package org.ipig.commons.helper;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.ipig.constants.SymbolCnst;

import java.util.HashMap;
import java.util.Map;

/**
 * ApiGtwHelper
 *
 * @author <a href="mailto:comchnts@163.com">chinats</a>
 * @version $$Id: ApiGtwHelper.java 16 2020-04-30 09:35:40Z ts <comchnts@163.com> $$
 * @since 1.0
 */
@Slf4j
public class ApiGtwHelper {
    public static final String APP_ID = "appId";
    public static final String BIZ_CODE = "bizCode";
    public static final String BIZ_ID = "bizId";
    public static final String APP_KEY = "appKey";
    public static final String CONTENT = "content";
    public static final String TIMESTAMP = "timestamp";
    public static final String SIGN = "sign";
    public static final String MESSAGE = "message";
    public static final String CODE = "code";

    /**
     * doPost
     *
     * @param url       String
     * @param appId     String
     * @param appKey    String
     * @param bizCode   String
     * @param bizId     String
     * @param content   String
     * @param timestamp String
     * @return String
     */
    public static String doPost(final String url, final String appId, final String appKey, final String bizCode,
                                final String bizId, final String content, final String timestamp) {
        String sign = getSign(bizCode, bizId, content, timestamp, appKey);
        Map<String, String> map = new HashMap<>();
        map.put(APP_ID, appId);
        map.put(BIZ_CODE, bizCode);
        map.put(BIZ_ID, bizId);
        map.put(CONTENT, content);
        map.put(TIMESTAMP, timestamp);
        map.put(SIGN, sign);
        String result = HttpHelper.doPost(url, map, HttpHelper.ENCODING);
        return result;
    }

    /**
     * getSign
     *
     * @param bizCode   String
     * @param bizId     String
     * @param content   String
     * @param timestamp String
     * @param appKey    String
     * @return String
     */
    public static String getSign(final String bizCode, final String bizId, final String content,
                                 final String timestamp, final String appKey) {
        StringBuilder sb = new StringBuilder();
        sb.append(BIZ_CODE).append(SymbolCnst.EQUALS).append(bizCode).append(SymbolCnst.AND);
        sb.append(BIZ_ID).append(SymbolCnst.EQUALS).append(bizId).append(SymbolCnst.AND);
        sb.append(CONTENT).append(SymbolCnst.EQUALS).append(content).append(SymbolCnst.AND);
        sb.append(TIMESTAMP).append(SymbolCnst.EQUALS).append(timestamp).append(SymbolCnst.AND);
        sb.append(APP_KEY).append(SymbolCnst.EQUALS).append(appKey);
        return DigestUtils.sha1Hex(sb.toString());
    }

    /**
     * 获取API网关的处理结果
     *
     * @param result String
     * @return boolean
     */
    public boolean getProcResult(final String result) {
        boolean flag = false;
        if (JSONObject.isValid(result)) {
            try {
                JSONObject resultJson = JSONObject.parseObject(result);
                if (resultJson != null) {
                    Object msg = resultJson.get(MESSAGE);
                    if (msg instanceof JSONObject) {
                        JSONObject msgRes = (JSONObject) msg;
                        Object codeObj = msgRes.get(CODE);
                        String code = codeObj == null ? StringUtils.EMPTY : codeObj.toString();
                        if (StringUtils.equals(code, "0")) {
                            flag = true;
                        }
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                log.error(org.apache.commons.lang.exception.ExceptionUtils.getFullStackTrace(ex));
            }
        }
        return flag;
    }
}